<?php

namespace App\Http\Controllers\Api\Cps;

use Illuminate\Http\Request;

use App\Models\Fx\CpsFenxiao;

use App\Models\Bill\Bill;

use App\Models\Bill\BillGoods;

use App\Models\Goods\GoodsSaleEdit;

use App\Http\Controllers\Controller;

class FxOrderController extends Controller
{

    public function search( Request $request )
    {
        $start_time = $request->input('start_time');
        $end_time = $request->input('end_time');
        if ( empty($start_time) ) {
            return response()->json(['code' => 100000, 'message' => '缺少参数：start_time']);
        }

        if ( empty($end_time) ) {
            return response()->json(['code' => 100001, 'message' => '缺少参数：end_time']);
        }

        $where = [
            ['createTime','>=',$start_time],
            ['createTime','<=',$end_time],
        ];

        //返回信息
        $return_data = [];

        $cps_data = CpsFenxiao::where($where)->get()->toArray();
        if ( $cps_data ) {
            foreach ( $cps_data as $item ) {

                //查询订单信息
                $bill_master = Bill::find($item['bill_no']);
                if ( !$bill_master) {
                    continue;
                }

                //查询订单商品信息
                $bill_goods = BillGoods::where('bill_no',$item['bill_no'])->get()->toArray();
                if ( !$bill_goods ) {
                    continue;
                }

                $goods_array = array() ;
                foreach ( $bill_goods as $good ) {

                    $good_sale_data = GoodsSaleEdit::select('goodsMainPic')->find($good['goods_postID']);

                    if ( $bill_master['bill_coupon_money'] > 0 ) {
                        $price = round($good['goods_purchasePrice'] * (1 - ($bill_master['bill_coupon_money']/$bill_master['bill_totalPaid'])), 2);
                    } else {
                        $price = $good['goods_purchasePrice'];
                    }

                    $goods_array[] = array(
                        'productCode' => $good['product_code'],
                        'goodsName' => $good['goods_name'],
                        'goodsAmount' => $good['goods_amount'],//销售数量
                        'goodsPrice' => $price,//实售价格
                        'picture' => isset($good_sale_data['goodsMainPic']) ? $good_sale_data['goodsMainPic'] : '',//商品图片
                    );
                }

                //订单状态
                if ( $bill_master['bill_status'] == 0 ) {
                    $bill_status = 1 ;
                } else if ( $bill_master['bill_status'] == 1 ) {
                    $bill_status = 2 ;
                } else if ( in_array($bill_master['bill_status'],array( 2,3,7,8)) ) {
                    $bill_status = 3 ;
                } else if ($bill_master['bill_status'] == 5 ) {
                    $bill_status = 5 ;
                } else {
                    $bill_status = 4 ;
                }

                //付款状态
                if ( $bill_master['pay_status'] == 0 ) {
                    $pay_status = 0 ;
                } else {
                    $pay_status = 1 ;
                }

                //订单退货状态
                if ( $bill_master['bill_return_status'] == 0 ) {
                    $bill_return_status = 1 ;
                } else if ( $bill_master['bill_return_status'] == 1 ) {
                    $bill_return_status = 2 ;
                } else {
                    $bill_return_status = 3 ;
                }

                //订单退款状态
                if ( $bill_master['bill_refund_status'] == 0 ) {
                    $bill_refund_status = 1 ;
                } else if ( $bill_master['bill_refund_status'] == 1  ) {
                    $bill_refund_status = 2 ;
                } else {
                    $bill_refund_status = 3 ;
                }

                //返回订单信息
                $return_data[] = array(
                    'bill_no' => $item['bill_no'],
                    'sid' => $item['sid'],//微店长iD
                    'billStatus' => $bill_status,
                    'userId' => $bill_master['pcustID'],//第三方会员编号
                    'userName' => $bill_master['custID'],//第三方会员名
                    'billPayStatus' => $pay_status,//支付状态
                    'billReturnStatus' => $bill_return_status,//订单退货状态
                    'billRefundStatus' => $bill_refund_status,//订单退款状态
                    'billReceiver' => $bill_master['deliver_name'],//订单收货人
                    'billTotalMoney' => $bill_master['bill_payable'],//订单实付金额
                    'mallCode' => $bill_master['mallCode'],//门店编号
                    'mallName' => $bill_master['mallName'],//门店名称
                    'goods' => $goods_array,//订单商品信息
                );
            }

        }

        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $return_data]);

    }

}